<?php
namespace backend\models;
use Yii;

/**
 * This is the model class for table "admin".
 *
 * @property string $userid
 * @property string $username
 * @property integer $password
 * @property string $email
 * @property string $phone
 */
class Admin extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'admin';
    }

    /**
     * @inheritdoc
     */
     // 设置场景
      public function scenarios()
    {
        return[
            'logo'=>['username','password','yzm','verifyPassword'],
            'look'=>['username','password','phone','email','yzm'],
            'wang'=>['email'],
                   ];
    }

    public $yzm;//验证码
    public $verifyPassword;//确定密码
    
    public function rules()
    {// 验证规则
        return [
        [['username','password','phone'],'required','message'=>'{attribute}不能为空'],
                 //正则判断用户
                 //字段不能为空;
                 [['username','password','verifyPassword','phone','email'],'required','message'=>'{attribute}不能为空','on'=>['look','logo']],
                 //正则判断用户
                
                 //密码正则判断
                 [['password'],'match','pattern'=>'/^(?!^\\d+$)(?!^[a-zA-Z]+$)(?!^[_#@]+$).{8,}$/','message'=>'{attribute}请输入最少四位之六位','on'=>['look','logo']],
                 ['verifyPassword', 'compare', 'compareAttribute'=>'password', 'message'=>'与上次密码不符','on'=>'logo'],
                 //唯一性
                 [['username','email','phone'],'unique','message'=>'已被注册','on'=>'look'],
                 //邮箱规则
                 ['email','match','pattern'=>'/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/','message'=>'{attribute}验证规则错误','on'=>['look','logo','wang']],
                 // 手机规则
                 ['phone','match','pattern'=>"/^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\\d{8}$/",'message'=>'手机格式错误','on'=>'look'],
                 // 邮箱判断
                 [['email'],'required','message'=>'{attribute}不能为空','on'=>['look','wang']],
                // 验证码
                 ['yzm', 'captcha','captchaAction'=>'index/captcha','message'=>'验证码输入错误了，傻是不是？','on'=>'logo'],  
                 ['yzm', 'captcha','captchaAction'=>'index/captcha','message'=>'验证码输入错误了，傻是不是？','on'=>'look'],  
        ];
    }
    /**
     * @inheritdoc
     */
    public function attributeLabels(){
        return [
            'username' =>'用户名',
            'password' =>'密码',
            'verifyPassword'=>'确定密码',
            'email' =>'邮箱',
            'phone' =>'手机号码',
            'yzm'=>'验证码'
        ];
    }


// 验证登录信息      
    public function on_logo(){

        $password=Sha1($this->password);

         // 查找用户名与密码
        $list=Admin::find()->where(['username'=>$this->username,'password'=>$password])->asArray()->one();

        if(empty($list)){
             
             return false;

        }else{
            $session=Yii::$app->session;
            $session['userid']=$list['userid'];
            $session['username']=$list['username'];
            $session['password']=$list['password'];
            return true;
           }
    }
  
//查看邮箱数据
    public function check(){

      $email=$this->email;//如果有这条数据;
      $loke=Admin::find()->where(['email'=>$this->email])->asArray()->one();

         if(!$loke){
             return false;
         }else{
             return true;
         }
    }
    
}
